package com.personal.gateway.mapper;

import com.personal.gateway.entity.FlinkGatewayRoute;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @program: FlinkGateway
 * @description:
 * @author: yangliao
 * @create: 2021-08-23
 */
@Mapper
public interface FlinkGatewayRouteMapper {

    @Select("select * from flink_gateway_route where del_flag = 0 order by id ")
    List<FlinkGatewayRoute> list();

    @Select("select * from flink_gateway_route where del_flag = 0 and route_id = #{routeId}")
    FlinkGatewayRoute getRouteByRouteId(@Param("routeId") String routeId);

    @Select("select * from flink_gateway_route where del_flag = 0 and id = #{id}")
    FlinkGatewayRoute getRouteById(@Param("id") Integer id);

    @Select("select count(*) from flink_gateway_route where route_id = #{routeId} and del_flag = 0")
    int countByRouteId(@Param("routeId") String routeId);

    @Select("select count(*) from flink_gateway_route where route_uri = #{routeUri} and del_flag = 0")
    int countByRouteUri(@Param("routeUri") String routeUri);

    @Update("UPDATE `flink_gateway_route` SET " +
            "   `user_name` = #{userName}, " +
            "   `del_flag` = '1' " +
            "WHERE " +
            "   `id` = #{id}")
    int delete(@Param("userName") String userName, @Param("id") Integer id);

    @Insert("INSERT INTO `flink_gateway_route` " +
            "(" +
            " `gateway_num`, `route_id`, `route_uri`, `route_predicates`, `route_filters_stripprefix`, " +
            " `project_summary`, `project_detail`, " +
            " `flink_version`, `start_class`, `image_name`, `task_manager_memory`, `start_jar_name`, `user_name`," +
            " `alarm_status`, `testing_interval`, `alarm_email`" +
            ") " +
            "VALUES " +
            "(" +
            " #{route.gatewayNum}, #{route.routeId}, #{route.routeUri}, #{route.routePredicates}, #{route.routeFiltersStripprefix}, " +
            " #{route.projectSummary}, #{route.projectDetail}, " +
            " #{route.flinkVersion}, #{route.startClass}, #{route.imageName}, #{route.taskManagerMemory}, #{route.startJarName}, #{route.userName}," +
            " #{route.alarmStatus}, #{route.testingInterval}, #{route.alarmEmail} " +
            ")")
    int insert(@Param("route") FlinkGatewayRoute route);

    @Insert("UPDATE `flink_gateway_route`  " +
            "SET  " +
            "   `alarm_status` = #{route.alarmStatus},  " +
            "   `testing_interval` = #{route.testingInterval},  " +
            "   `alarm_email` = #{route.alarmEmail}  " +
            "WHERE  " +
            " `gateway_num` = #{route.gatewayNum} " +
            " AND `del_flag` = 0")
    int updateAlarm(@Param("route") FlinkGatewayRoute route);

}
